﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Castle.ActiveRecord;
using NHibernate.Criterion;
using LIB;

public partial class admin_tap_chi_Editor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            using (new SessionScope())
            {
                if (!IsPostBack)
                {
                    litPos.Text = "<a href='/admin/tap-chi/default.aspx'>Module Quản trị tạp chí</a> >>";
                    int id = ConvertUtility.ToInt32(Request.QueryString["id"]);

                    if (id > 0)
                    {
                        hidID.Value = id.ToString();
                        initForm(true);
                    }
                    else
                    {
                        initForm(false);
                    }
                }
            }

            fail.Visible = false;
        }
        catch (Exception ex) { }
    }
    protected void initForm(bool isEdit)
    {
        if (isEdit)
        {
            btnAdd.Visible = false;
            btnEdit.Visible = true;
            litPos.Text += "Sửa ";
            fillData();
        }
        else
        {
            btnAdd.Visible = true;
            btnEdit.Visible = false;
            litPos.Text += "Thêm mới  ";
        }
    }


    protected void fillData()
    {
        try
        {
            tapchi_Info tapChiInfo = tapchi_Info.Find(ConvertUtility.ToInt32(hidID.Value));
            imgAvatar.ImageUrl = App_Constant.PATH_UP_IMAGES_SLIDE + "/" + tapChiInfo.tcAvatar;
            hidAvatar.Value = tapChiInfo.tcAvatar;
            txtTitle.Text = tapChiInfo.tcName;
            txtDes.Text = tapChiInfo.tcLink;
            
        }
        catch (Exception ex) { }
    }
    protected tapchi_Info getTapChiInfo(tapchi_Info tapchiInfo)
    {
        tapchiInfo.tcAvatar = fileUpAvatar.HasFile ? App_File.tienToFile() + "." + fileUpAvatar.FileName : hidAvatar.Value;
        tapchiInfo.tcName = txtTitle.Text;
        tapchiInfo.tcLink = txtDes.Text;
        tapchiInfo.tcDateTime = DateTime.Now;
        return tapchiInfo;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            tapchi_Info tapchiInfo = getTapChiInfo(new tapchi_Info());
            if (fileUpAvatar.HasFile)
                App_File.uploadFileOnSerVer(fileUpAvatar, App_Constant.PATH_UP_IMAGES_SLIDE + "/", 150, 100);
            using (new SessionScope())
                tapchiInfo.Create();
            Response.Redirect("/admin/tap-chi/default.aspx");
        }
        catch (Exception ex)
        {
            fail.Visible = true;
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            tapchi_Info tapchiInfo = getTapChiInfo(tapchi_Info.Find(ConvertUtility.ToInt32(hidID.Value)));
            if (fileUpAvatar.HasFile)
                App_File.uploadFileOnSerVer(fileUpAvatar, App_Constant.PATH_UP_IMAGES_SLIDE + "/", 150, 100);
            using (new SessionScope())
                tapchiInfo.SaveAndFlush();
            Response.Redirect("/admin/tap-chi/default.aspx");
        }
        catch (Exception ex)
        {
            fail.Visible = true;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (hidID.Value != "")
            fillData();
        else
            ControlUtility.EmptyTextBoxes(this);

    }
}